package com.jtg.security02.service;

import com.jtg.security02.mapper.UserMapper;
import com.jtg.security02.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户详情
 */
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //用户信息查询
        User user = userMapper.loadUserByUserName(s);
        if (user == null) {
            throw new UsernameNotFoundException("该用户不存在");
        }
        //用户角色
        user.setRoles(userMapper.getUserRolesByUserId(user.getId()));
        return user;
    }
}
